package com.ruoyi.system.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.VipUser;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.mapper.TopicMapper;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Map;
import java.util.stream.Collectors;

import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.system.domain.Topic;
import com.ruoyi.system.mapper.PoolMapper;
import com.ruoyi.system.domain.Pool;
import com.ruoyi.system.service.IPoolService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import static com.ruoyi.common.utils.ShiroUtils.getLoginName;

/**
 * 题库管理Service业务层处理
 *
 * @author ruoyi
 * @date 2023-01-12
 */
@Service
public class PoolServiceImpl implements IPoolService {
    @Autowired
    private PoolMapper poolMapper;

    @Autowired
    private TopicMapper topicMapper;

    @Autowired
    private SysUserMapper userMapper;


    /**
     * 查询题库管理
     *
     * @param poolId 题库管理主键
     * @return 题库管理
     */
    @Override
    public Pool selectPoolByPoolId(Long poolId) {
        return poolMapper.selectPoolByPoolId(poolId);
    }

    /**
     * 查询题库id
     *
     * @return 题库id管理
     */
    @Override
    public List<String> selectPoolName() {
        return poolMapper.selectPoolName();
    }

    /**
     * 查询题库管理列表
     *
     * @param pool 题库管理
     * @return 题库管理
     */
    @Override
    public List<Pool> selectPoolList(Pool pool) {
        String loginName = getLoginName();
        VipUser user = userMapper.selectVipUserByLoginName(loginName);
        if (!user.isAdmin()){
            pool.setCreateBy(loginName);
            pool.setIsVip(user.getIsVip());
        }
        return poolMapper.selectAdminPoolList(pool);
    }

    /**
     * 查询展示给用户的题库管理列表
     *
     * @param pool 题库管理
     * @return 题库管理
     */
    @Override
    public List<Pool> selectUserPoolList(Pool pool) {
        return poolMapper.selectUserPoolList(pool);
    }

    /**
     * 新增题库管理
     *
     * @param pool 题库管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertPoolByPrimaryChildTable(Pool pool) {
        pool.setCreateTime(DateUtils.getNowDate());
        pool.setCreateBy(getLoginName());
        int rows = poolMapper.insertPool(pool);
        insertTopic(pool);
        return rows;
    }

    /**
     * 新增题库管理
     *
     * @param pool 题库管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertPool(Pool pool) {
        pool.setCreateTime(DateUtils.getNowDate());
        pool.setCreateBy(getLoginName());
        int rows = poolMapper.insertPool(pool);
        //insertTopic(pool);
        return rows;
    }

    /**
     * 修改题库管理
     *
     * @param poolVo 题库管理
     * @return 结果
     */
    @Transactional
    @Override
    public int updatePool(Pool poolVo) {
        boolean isUpdate = false;
        if (!poolVo.getPoolName().equals(selectPoolByPoolId(poolVo.getPoolId()).getPoolName())){
            isUpdate = true;
        }
        poolVo.setUpdateTime(DateUtils.getNowDate());
        String loginName = getLoginName();
        poolVo.setUpdateBy(loginName);
        List<Topic> topicList = poolVo.getTopicList();
        if (topicList != null){
            for (Topic topic : topicList) {
                if (topic.getTopicId() == null) {
                    topic.setPoolId(poolVo.getPoolId());
                    topic.setCreateTime(DateUtils.getNowDate());
                    topic.setCreateBy(loginName);
                    topic.setLastDoneState("0");
                    topicMapper.insertTopic(topic);
                    isUpdate = true;
                } else if (topic.equals(topicMapper.selectTopicByTopicId(topic.getTopicId()))) {
                    topic.setUpdateTime(DateUtils.getNowDate());
                    topic.setUpdateBy(getLoginName());
                    topicMapper.updateTopic(topic);
                    isUpdate = true;
                }
            }
        }
        if (isUpdate == false){
            poolVo.setUpdateBy(null);
            poolVo.setUpdateTime(null);
        }
        return poolMapper.updatePool(poolVo);
    }

    /**
     * 批量删除题库管理
     *
     * @param poolIds 需要删除的题库管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deletePoolByPoolIds(String poolIds) {
        poolMapper.deleteTopicByPoolIds(Convert.toStrArray(poolIds));
        return poolMapper.deletePoolByPoolIds(Convert.toStrArray(poolIds));
    }

    /**
     * 删除题库管理信息
     *
     * @param poolId 题库管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deletePoolByPoolId(Long poolId) {
        poolMapper.deleteTopicByPoolId(poolId);
        return poolMapper.deletePoolByPoolId(poolId);
    }

    /**
     * 新增题目管理信息
     *
     * @param pool 题库管理对象
     */
    public void insertTopic(Pool pool) {
        List<Topic> topicList = pool.getTopicList();
        Long poolId = pool.getPoolId();
        if (StringUtils.isNotNull(topicList)) {
            List<Topic> list = new ArrayList<Topic>();
            for (Topic topic : topicList) {
                topic.setPoolId(poolId);
                topic.setCreateTime(DateUtils.getNowDate());
                topic.setCreateBy(getLoginName());
                topic.setLastDoneState("0");
                list.add(topic);
            }
            if (list.size() > 0) {
                poolMapper.batchTopic(list);
            }
        }
    }
}
